//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "MethodWeighted.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fstream>
#include <iostream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define MAX_SIZE 500000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
MethodWeighted::MethodWeighted () {

  this->modelText               = new String ();
  this->patterns                = new TListE <PatternWeight> ();
  this->correction              = 0;
  
  this->toString                = new String ();

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
MethodWeighted::MethodWeighted (const MethodWeighted& methodWeighted) {

  this->modelText               = new String (methodWeighted.modelText);
  this->patterns                = new TListE <PatternWeight> (methodWeighted.patterns);
  this->correction              = methodWeighted.correction;

  this->toString                = new String (methodWeighted.toString);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
MethodWeighted::MethodWeighted (const MethodWeighted* methodWeighted) {

  this->modelText               = new String (methodWeighted->modelText);
  this->patterns                = new TListE <PatternWeight> (methodWeighted->patterns);
  this->correction              = methodWeighted->correction;

  this->toString                = new String (methodWeighted->toString);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
MethodWeighted::~MethodWeighted () {

  if (this->modelText) delete this->modelText;
  if (this->patterns)  delete this->patterns;
  
  if (this->toString)  delete this->toString;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void MethodWeighted::ModelText (String* modelText) {

  if (modelText)
    *(this->modelText) = *modelText;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void MethodWeighted::Patterns (TListE <PatternWeight>* patterns) {

  if (patterns)
    *(this->patterns) = *patterns;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void MethodWeighted::Correction (float correction) {

  this->correction = correction;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

String* MethodWeighted::ModelText (void) {

  return this->modelText;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <PatternWeight>* MethodWeighted::Patterns (void) {

  return this->patterns;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
float MethodWeighted::Correction (void) {

  return this->correction;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool MethodWeighted::LoadData (String* data) {

  if (!data) return false;
  
  return this->LoadData(data->Out());
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool MethodWeighted::LoadData (char* data) {

  if (!data) return false;

  TListE <String>* lines = new TListE <String> ();
  char*            parts = NULL;

  parts = strtok(data, "\n");
  while (parts) {
    lines->Add(new String(parts));
    parts = strtok(NULL, "\n");
  }  

  lines->SetInitial();
  while (lines->SetNext()) {
    
    char* line = lines->GetCurrent()->Out();
    
    if ((strstr(line, "*") || strstr(line, "+"))) {
      
      PatternWeight* newPatternWeight = new PatternWeight();
      
      if (newPatternWeight->ReadFromString(line)) {
        if (!strcmp(newPatternWeight->Type()->Out(), "Correction"))
          this->correction = newPatternWeight->Weight();
        this->patterns->Add(newPatternWeight);
      }
      else
        delete newPatternWeight;
      
    }
    else continue;
    
  }
  
  if (lines) delete lines;
  
  return true;
  

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
MethodWeighted MethodWeighted::operator= (const MethodWeighted& methodWeighted) {

  *(this->modelText) = *(methodWeighted.modelText);
  *(this->patterns)  = *(methodWeighted.patterns);
  this->correction   = methodWeighted.correction;
  
  *(this->toString)  = *(methodWeighted.toString);

  return *this;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
